//
//  PGTimeView.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-19.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "PGTimeView.h"
#import "EpointDateUtils.h"
#import "UIView+Addtions.h"

@interface PGTimeView ()

@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *timeLabel;

@end

@implementation PGTimeView

- (id)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame withTap:YES];
}

- (id)initWithFrame:(CGRect)frame withTap:(BOOL)animated {
    self = [super initWithFrame:frame];
    if (self) {
        self.dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_dateLabel];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_timeLabel];
        
        [self setDefaultValue];
        
        if (animated) {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeViewtapAction:)];
            [self addGestureRecognizer:tap];
        }
    }
    return self;
}

- (void)setDefaultValue {
    _timeLabel.font = [UIFont boldSystemFontOfSize:18];
}

- (void)setTimeDate:(NSDate *)timeDate {
    _timeDate = timeDate;
    NSString *dateStr = [EpointDateUtils convertDate2FormatStr:_timeDate format:@"yyyy-MM-dd"];
    NSString *timeStr = [EpointDateUtils convertDate2FormatStr:_timeDate format:@"HH:mm"];
    _dateLabel.text = dateStr;
    _timeLabel.text = timeStr;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [_timeLabel sizeToFit];
    _timeLabel.centerY = self.height*0.5;
    _timeLabel.right = self.width - 20;
    
    [_dateLabel sizeToFit];
    _dateLabel.centerY = self.height*0.5;
    _dateLabel.left = 10;
    _dateLabel.width = _timeLabel.left - 15;
}

- (void)timeViewtapAction:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateRecognized) {
        if (_delegate && [_delegate respondsToSelector:@selector(timeViewPressed:)]) {
            [_delegate timeViewPressed:self];
        }
    }
}

@end
